//
//  WXLBaseViewController.m
//  段子手
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "WXLBaseViewController.h"

@interface WXLBaseViewController ()

@property (nonatomic, retain) TouchMoveView *touchMovieView;

@property (nonatomic, retain) CABasicAnimation *animation; /**< 动画 */
@property (nonatomic, strong) UIVisualEffectView *visualEfView; /**< 毛玻璃 */
@property (nonatomic, retain) UIButton *leftReturnButton;

@end

@implementation WXLBaseViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

/** 创建返回item */
- (void)createLeftItem {
    self.leftReturnButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _leftReturnButton.frame = CGRectMake(10, 30, 30, 30);
    [self.view addSubview:_leftReturnButton];
    [_leftReturnButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-4"] forState:(UIControlStateNormal)];
    [_leftReturnButton addTarget:self action:@selector(leftReturnItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

/** 创建可拖动的view */
- (void)createTouchMoveView {
    
    self.touchMovieView = [[TouchMoveView alloc] initWithFrame:CGRectMake(VIEW_CENTER_X - (kUIScreenWidth) / 2, kUIScreenHeight - 60, kUIScreenWidth, 60)];
    [self.view addSubview:_touchMovieView];
    _touchMovieView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wxl"]];
    
    // touch上的毛玻璃
    [self createVisualViewForTouch];
    
    // 动图
    [self createAnimationView];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // 设置轻扫方向
    swipe.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
    [_touchMovieView addGestureRecognizer:swipe];
}

/** touch上的毛玻璃 */
- (void)createVisualViewForTouch {
    // 毛玻璃
    UIVisualEffectView *visualEfV = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visualEfV.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    visualEfV.alpha = 1;
    visualEfV.userInteractionEnabled = YES;
    
    [self.touchMovieView addSubview:visualEfV];
}

/** 毛玻璃 */
- (void)createVisualEfView {
    // 毛玻璃
    self.visualEfView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    _visualEfView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    _visualEfView.alpha = 1;
    _visualEfView.userInteractionEnabled = YES;
    
    [self.view addSubview:_visualEfView];
    
}

- (void)createAnimationView {
    
    self.imgView = [[UIImageView alloc] init];
    _imgView.frame = CGRectMake(_touchMovieView.frame.size.width / 2 - (_touchMovieView.frame.size.height - 10) / 2, 5, _touchMovieView.frame.size.height - 10, _touchMovieView.frame.size.height - 10);
    [self.touchMovieView addSubview:_imgView];
    _imgView.layer.cornerRadius = _imgView.frame.size.height / 2;
    _imgView.layer.masksToBounds = YES;
    _imgView.userInteractionEnabled = YES;
    
    if (_myModel.picUrl.length) {
        [_imgView HANDLEIMAGE(_myModel.picUrl)];
    } else {
        _imgView.image = [UIImage imageNamed:@"beijing.jpg"];
    }
    
    // 保证安全
    if (!self.animation) {
        // CALayer 动画
        // Core Animtion
        self.animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    }
    // 旋转的起始值
    _animation.fromValue = [NSNumber numberWithFloat:0];
    // 旋转的结束值
    // π 是 180°  2π 是 360°
    _animation.toValue = [NSNumber numberWithFloat:M_PI * 2];
    // 重复次数
    _animation.repeatCount = NSIntegerMax;
    // 时间
    _animation.duration = 5;
    // 是否自动回到原来位置
    _animation.autoreverses = NO;
    // 是否按照结束位置继续旋转
    _animation.cumulative = YES;
    
    [_imgView.layer addAnimation:_animation forKey:@"ddd"];
    
}




#pragma mark - 点击
- (void)leftReturnItemAction:(UIBarButtonItem *)item {
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark - 轻扫
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
    
    [self presentViewController:[WXLPlayMusicViewController shareWXLPlayMusicViewController] animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets  = NO;
    
    self.view.backgroundColor = kBackgroundColor;
    
    self.navigationController.navigationBar.translucent = YES;
    
    [self createVisualEfView];
    
    [self createTouchMoveView];
    
    [self createLeftItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    // 保证安全
    if (!self.animation) {
        self.animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    }
    // 旋转的起始值
    _animation.fromValue = [NSNumber numberWithFloat:0];
    // 旋转的结束值
    // π 是 180°  2π 是 360°
    _animation.toValue = [NSNumber numberWithFloat:M_PI * 2];
    // 重复次数
    _animation.repeatCount = NSIntegerMax;
    // 时间
    _animation.duration = 5;
    // 是否自动回到原来位置
    _animation.autoreverses = NO;
    // 是否按照结束位置继续旋转
    _animation.cumulative = YES;
    
    [_imgView.layer addAnimation:_animation forKey:@"ddd"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
